package com.lfs.admin.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.lfs.admin.model.dto.PlatformDto;
import com.lfs.admin.model.dto.ShopDto;
import com.lfs.admin.service.PlatformService;
import com.lfs.base.aspect.UnLogAspect;
import com.lfs.base.dto.Result;
import com.lfs.base.dto.ResultObject;
import com.lfs.common.annotation.Log;
import com.lfs.common.core.controller.BaseController;
import com.lfs.common.core.domain.AjaxResult;
import com.lfs.common.core.page.TableDataInfo;
import com.lfs.common.enums.BusinessType;
import com.lfs.dao.entity.PageBean;
import com.lfs.interfaces.dto.TokenCreateDto;
import com.lfs.interfaces.model.PlatformEntity;
import com.lfs.interfaces.model.vo.LogisticsTemplateVo;
import com.lfs.interfaces.model.vo.TokenCreateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/platform")
@Slf4j
@Api(value = "平台管理", tags = {"平台管理"})
public class PlatFormController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(PlatFormController.class);

    @Autowired
    private PlatformService platformService;

    @Log(title = "查询平台列表", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('platform:list')")
    @ApiOperation(value = "查询平台列表")
    @PostMapping("/list")
    public Result<?> list(@RequestBody PlatformDto platformDto) {
        logger.info("查询平台列表请求开始");
        List<PlatformEntity> platFormList = platformService.queryPlatFormList(platformDto);
        return ResultObject.successObject(platFormList,"success");
    }

    @PostMapping("/getOauthUrl")
    @ApiOperation(value = "获取平台授权url")
    @UnLogAspect
    public AjaxResult oauth(@RequestBody TokenCreateDto tokenCreateDto) {
        String url = platformService.getAuthUrl(tokenCreateDto);
        return AjaxResult.success("SUCCESS",url);
    }

    @PostMapping("/getAccessToken")
    @ApiOperation(value = "获取accessToken")
    @UnLogAspect
    public Result<?> getToken(@RequestBody TokenCreateDto tokenCreateDto) throws IOException, InterruptedException {
        log.info("/**********获取对应平台的授权token*********/");
        if(StrUtil.isEmpty(tokenCreateDto.getCode())){
            return ResultObject.serverErrorObject("请填写平台获取的授权code!");
        }
        return ResultObject.successObject(platformService.getAccessToken(tokenCreateDto),"success");
    }

    @PostMapping("/getMallInfo")
    @ApiOperation(value = "根据token获取店铺信息")
    @UnLogAspect
    public Result<?> getMallInfo(@RequestBody TokenCreateDto tokenCreateDto) throws IOException, InterruptedException {
        log.info("/**********根据token获取店铺信息*********/");
        if(StrUtil.isEmpty(tokenCreateDto.getAccessToken())){
            return ResultObject.serverErrorObject("请填写平台获取的授权accessToken!");
        }
        return ResultObject.successObject(platformService.getMallInfo(tokenCreateDto),"success");
    }

    @PostMapping("/getGoodsCats")
    @ApiOperation(value = "获取商品类目信息")
    @UnLogAspect
    public Result<?> getGoodsCats(){
        log.info("/**********获取对应平台的商品类目信息*********/");
        JSONObject jsonObject = platformService.getGoodsCats();
        return ResultObject.successObject(jsonObject,"success");
    }

    @PostMapping("/getGoodsCatRule")
    @ApiOperation(value = "获取商品类目发布规则")
    @UnLogAspect
    public Result<?> getGoodsCatRule(){
        log.info("/**********获取对应平台的商品类目发布规则*********/");
        JSONObject jsonObject = platformService.getGoodsCats();
        return ResultObject.successObject(jsonObject,"success");
    }

    @PostMapping("/getLogisticsTemplate")
    @ApiOperation(value = "获取商品运费模版信息")
    @UnLogAspect
    public Result<?> getGoodsTemplate(){
        log.info("/**********获取对应平台的商品运费模版信息*********/");
        List<LogisticsTemplateVo> list = platformService.getLogisticsTemplate();
        return ResultObject.successObject(list,"success");
    }

    @PostMapping("/releaseProduct")
    @ApiOperation(value = "发布商品到对应的平台")
    public AjaxResult releaseProduct(@RequestBody TokenCreateDto tokenCreateDto) throws IOException, InterruptedException {
        log.info("/**********发布商品到对应的平台*********/");
        platformService.releaseProduct();
        return AjaxResult.success("SUCCESS");
    }

    @PostMapping("/getPageCode")
    @ApiOperation(value = "开平ISV前端插件检测页面code获取接口")
    public AjaxResult getPageCode(@RequestBody JSONObject jsonObject) throws IOException, InterruptedException {
        log.info("/**********发布商品到对应的平台*********/");
        return AjaxResult.success(platformService.getPageCode(jsonObject));
    }
}
